export default {
  /**
   *
   * @param {*} code 代码
   * @param {*} time 速度
   * @param {*} box 绘制容器
   * @param {*} stage 绘制舞台
   */
  init (code = '猛哥', time = 2000, box = '.home', stage = '#q') {
    let timer
    const go = () => {
      const q = document.querySelector(stage)
      const b = document.querySelector(box)
      const s = b.getBoundingClientRect()
      const w = (q.width = s.width)
      const h = (q.height = s.height)
      const ctx = q.getContext('2d')

      const p = Array(Math.floor(w / 10) + 1).fill(0)

      const random = (items) => items[Math.floor(Math.random() * items.length)]

      const hex = code.split('')
      return setInterval(() => {
        ctx.fillStyle = 'rgba(0,0,0,.05)'
        ctx.fillRect(0, 0, w, h)
        ctx.fillStyle = '#0f0'
        p.map((v, i) => {
          ctx.fillText(random(hex), i * 10, v)
          p[i] = v >= h || v > 50 + 10000 * Math.random() ? 0 : v + 10
        })
      }, time / 30)
    }
    timer = go()
    window.onresize = () => {
      clearInterval(timer)
      timer = go()
    }
  }
}
